package com.cx.Controller.back;


import com.alibaba.dubbo.config.annotation.Reference;
import com.cx.service.IArticleCommentService;
import com.cx.service.IArticleService;
import com.cx.service.IClubService;
import com.cx.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cx
 * @since 2024-06-07
 */
@CrossOrigin    //解决跨域问题注解
@Controller
//@RequestMapping("/")
public class BackHomeController {

    //配置基于权重的轮询算法
    @Reference(loadbalance = "roundrobin")  //文章
    private IArticleService articleService;
    //配置基于权重的轮询算法
    @Reference(loadbalance = "roundrobin")  //文章评论
    private IArticleCommentService articleCommentService;
    //配置基于权重的轮询算法
    @Reference(loadbalance = "roundrobin")  //社团
    private IClubService clubService;
    //配置基于权重的轮询算法
    @Reference(loadbalance = "roundrobin")  //用户
    private IUserService userService;

    //    文件上传地址
    final String filepathDir = "D:\\Dubbo\\snowman-forum-blog-Projects\\SnowmanForumBlogSpringBoot\\ForumBlog\\src\\main\\resources\\static\\back\\images\\";

    //     博客前台用户端首页
//    @GetMapping({"/","/page/{pageNo}"})
//    public String toUserIndex(Model model, @PathVariable(required = false) Integer pageNo){
//        PageHelper.startPage(pageNo==null?1:pageNo,5);
//        QueryWrapper<Article> articleQueryWrapper = new QueryWrapper<>();
//        articleQueryWrapper.orderByDesc("created");
//        PageInfo<Article> pageInfo = new PageInfo<>(articleService.list(articleQueryWrapper));
//        model.addAttribute("articles", pageInfo);
//        model.addAttribute("articleList", articleService.getHotArticle());
//        return "client/index";
//    }
    //==================================================
    //    管理员登录接口
    @GetMapping({"/", "/login"})
    public String login() {
        return "back/page/user/login";
    }

    //    进入后台管理首页
    @GetMapping({"/home"})
    public String toAdminIndex() {
        return "back/index";
    }
    //    个人信息：进入编辑页面（携带数据）
    @GetMapping("page/user-setting.html")
    public String toAdminSetting(@RequestParam("id") Integer id, Model model) {
        model.addAttribute("user", userService.getById(id));
        return "back/page/user/user-setting";
    }

    //==================================================
    //    主页模板
    //       主页视图-1：携带数据
    @GetMapping("/page/welcome-1.html")
    public String welcome1(Model model) {
        Map<String, Long> welcome1Map = new HashMap<>();
//        用户人数统计
        welcome1Map.put("userStatistics", userService.count());
//        社团数量统计
        welcome1Map.put("clubStatistics", clubService.count());
//        文章数量统计
        welcome1Map.put("articleStatistics", articleService.count());
//        评论数量统计
        welcome1Map.put("articleCommentStatistics", articleCommentService.count());
        System.out.println("welcome1Map = " + welcome1Map);
        model.addAllAttributes(welcome1Map);

        return "back/page/welcome-1";
    }
    @GetMapping("/page/welcome-2.html")
    public String welcome2() {
        return "back/page/welcome-2";
    }
    @GetMapping("/page/welcome-3.html")
    public String welcome3() {
        return "back/page/welcome-3";
    }

    //==================================================
//    富文本编辑
    @GetMapping("/page/editor.html")
    public String toEditorPage() {
        return "back/page/wang-editor";
    }

    //==================================================
    //    文件上传页面
    @GetMapping("/page/upload.html")
    public String toUploadPage() {
        return "back/page/user/upload";
    }
//    文件上传接口

    @RequestMapping("/page/api/upload")
    public @ResponseBody String uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        //旧的文件名称
        String oldFilename = file.getOriginalFilename();
        //获取文件扩展名
        assert oldFilename != null;
        String extname = oldFilename.substring(file.getOriginalFilename().lastIndexOf("."));
        //防止重名
        String newFileName = oldFilename + "_" + UUID.randomUUID() + extname;
        //返回是整个文件的大小
        String savePath = filepathDir + newFileName;
        File file1 = new File(savePath);
        if (!file1.exists()) {
            file1.createNewFile();
        }
        if (file.getSize() != FileCopyUtils.copy(file.getInputStream(), Files.newOutputStream(new File(savePath).toPath()))) {
            return "文件上传失败！";
        } else {
            return savePath;
        }
    }


    //==================================================
    //    文章表格
    @GetMapping("/page/tableArticle.html")
    public String tableBackArticlePage() {
        return "back/page/article/list";
    }

    //    进入到文章添加页面的接口
    @GetMapping("/back-article/add.html")
    public String toBackArticleAdd() {
        return "back/page/article/add";
    }

    //    进入编辑页面：携带数据
    @GetMapping("/back-article/edit.html")
    public String toBackArticleEdit(@RequestParam Integer id, Model model) {
        model.addAttribute("article", articleService.getById(id));
        return "back/page/article/edit";
    }

    //==================================================
    //    文章评论表格
    @GetMapping("/page/tableArticleComment.html")
    public String tableArticleCommentPage() {
        return "back/page/articleComment/list";
    }

    //    进入到文章评论添加页面的接口
    @GetMapping("/back-articleComment/add.html")
    public String toArticleCommentAdd() {
        return "back/page/articleComment/add";
    }

    //    进入编辑页面：携带数据
    @GetMapping("/back-articleComment/edit.html")
    public String toArticleCommentEdit(@RequestParam Integer id, Model model) {
        model.addAttribute("articleComment", articleCommentService.getById(id));
        return "back/page/articleComment/edit";
    }

    //==================================================
    //    社团表格
    @GetMapping("/page/tableClub.html")
    public String tableClubPage() {
        return "back/page/club/list";
    }

    //    进入到社团添加页面的接口
    @GetMapping("/back-club/add.html")
    public String toClubAdd() {
        return "back/page/club/add";
    }

    //    进入编辑页面：携带数据
    @GetMapping("/back-club/edit.html")
    public String toClubEdit(@RequestParam Integer id, Model model) {
        model.addAttribute("club", clubService.getById(id));
        return "back/page/club/edit";
    }

    //==================================================
    //    用户表格
    @GetMapping("/page/tableUser.html")
    public String tableBackUserPage() {
        return "back/page/user/list";
    }
    //    进入到用户添加页面的接口
    @GetMapping("/back-user/add.html")
    public String toBackUserAdd() {
        return "back/page/user/add";
    }

    //    进入编辑页面：携带数据
    @GetMapping("/back-user/edit.html")
    public String toBackUserEdit(@RequestParam Integer id, Model model) {
        model.addAttribute("user", userService.getById(id));
        return "back/page/user/edit";
    }
    //==================================================


}
